<template>
	<view>
		<view class="goods_item">
			<!-- 商品左侧图片区域 -->
			<view class="goods-item-left">
				<radio :checked="goods.goods_state" color="#FF0000" v-if="showRadio" @click = "radioClickHandler"></radio>
				<image class="goods-pic" :src="goods.goods_small_logo || defaultPic">
			</view>
			
			<!-- 商品右侧信息区域 -->
			<view class="goods-item-right">
				<!-- 商品标题 -->
				<view class="goods-name">
					{{goods.goods_name}}
				</view>
				<view class="goods-info-box">
					<!-- 商品价格 -->
					<view class="goods-price">¥{{goods.goods_price | tofixed }} </view>
					<!-- 商品数量 -->
					<uni-number-box :min="1" :value="goods.goods_count" v-if="showNum" @change="numChangeHandler"></uni-number-box>
				</view>
			</view>
		</view>	
	</view>
</template>

<script>
	export default {
		name:"my-goods",
		props: {
			goods: {
				type: Object,
				default: []
			},
			showRadio: {
				type: Boolean,
				default: false
			},
			showNum: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				defaultPic: 'https://img2.woyaogexing.com/2020/11/03/7173435f4d2d42cab59ba5a9472fd0c0!400x400.jpeg'
			};
		},
		filters: {
			tofixed(num) {
				// 使用过滤器添加两位小数点
				return Number(num).toFixed(2)
			}
		},
		methods: {
			// radio组件的点击时间函数
			radioClickHandler() {
				this.$emit('radio-change', {
					goods_id: this.goods.goods_id,
					goods_state: !this.goods.goods_state,
				})
			},
			// 监听到numberbox数量变化的事件
			numChangeHandler(val) {
				this.$emit('num-change', {
					// 商品id
					goods_id: this.goods.goods_id,
					// 商品最新数量
					goods_count: +val
				})
			}
		}
	}
</script>

<style lang="scss">
	.goods_item {
		width: 750rpx;
		box-sizing:border-box;
		display: flex;
		border-bottom: 1px solid #F0F0F0;;
		padding: 10px 5px;
		.goods-item-left {
			display: flex;
			justify-content: space-between;
			align-items: center;
			.goods-pic {
				width: 100px;
				height: 100px;
			}
		}
		.goods-item-right {
			margin-left: 5px;
			font-size: 14px;
			// flex布局 按列两端对齐
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			.goods-name {
				font-family: "黑体";
				line-height: 18px;
			}
			.goods-info-box {
				display: flex;
				align-items: center;
				justify-content: space-between;
				.goods-price {
					color: #FB3B49;
					font-weight: bold;
				}
			}
		}
	}

</style>
